package com.tzy.newbank.service.impl;

import com.tzy.newbank.AOPlog.AOPTag;
import com.tzy.newbank.Pojo.Banklog;
import com.tzy.newbank.Pojo.Bankuser;
import com.tzy.newbank.mapper.BanklogMapper;
import com.tzy.newbank.mapper.BankuserMapper;
import com.tzy.newbank.service.IBankuserService;
import com.tzy.newbank.utils.BusinessException;
import com.tzy.newbank.utils.NoPowerException;
import com.tzy.newbank.utils.LoginException;
import com.tzy.newbank.utils.TransferErrorException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tzy
 * @since 2024-07-15
 */
@Service
@RequiredArgsConstructor
public class BankuserServiceImpl implements IBankuserService {
    private final BankuserMapper bankuserMapper;
    private final BanklogMapper banklogMapper;

    @Override
    public List<Bankuser> seleteAll() throws BusinessException {
        return bankuserMapper.seleteAll();
    }

    @AOPTag
    @Override
    public Boolean deposit(Bankuser bankuser, Double money) throws BusinessException {
        if (money < 0) {
            throw new BusinessException("存款金额不可为负");
        }
        Bankuser bankuser1 = bankuserMapper.selectByName(bankuser.getName());
        if (bankuser1.getState() == 1) {
            throw new BusinessException("账户已被冻结");
        }
        bankuserMapper.deposit(money, bankuser.getName());
        banklogMapper.saveLog(new Banklog(bankuser.getName(), "存钱：" + money, LocalDateTime.now()));
        return true;
    }

    @AOPTag
    @Override
    public Boolean withdrawals(Bankuser bankuser, Double money) throws BusinessException {
        if (money < 0) {
            throw new BusinessException("取款金额不可为负");
        }
        Bankuser bankuser1 = bankuserMapper.selectByName(bankuser.getName());
        if (bankuser1.getBalance() < money) {
            throw new BusinessException("余额不足");
        }
        if (bankuser1.getState() == 1) {
            throw new BusinessException("账户已被冻结");
        }
        bankuserMapper.withdraw(money, bankuser.getName());
        banklogMapper.saveLog(new Banklog(bankuser.getName(), "取钱：" + money, LocalDateTime.now()));
        return true;
    }

    @AOPTag
    @Override
    public Boolean transfer(Bankuser bankuser, Bankuser bankuser2, Double money) throws BusinessException, TransferErrorException {
        if ((bankuser.getName().equals(bankuser2.getName())) || (!exist(bankuser2))) {
            throw new TransferErrorException();
        }
        if (bankuser.getBalance() < 0) {
            throw new BusinessException("转账金额不可为负");
        }
        bankuserMapper.withdraw(money, bankuser.getName());
        bankuserMapper.deposit(money, bankuser2.getName());
        banklogMapper.saveLog(new Banklog(bankuser.getName(), "转账给" + bankuser2.getName() + "：" + money, LocalDateTime.now()));
        return true;
    }

    @Override
    public Double inquiry(Bankuser bankuser) {
        return bankuserMapper.selectByName(bankuser.getName()).getBalance();
    }

    @Override
    public Bankuser inquiryUser(Bankuser bankuser) {
        return bankuserMapper.selectByName(bankuser.getName());
    }

    @Override
    public Boolean register(Bankuser bankuser) throws LoginException {
        if (exist(bankuser)) {
            throw new LoginException("用户名已存在");
        }
        if (bankuser.getBalance() < 0) {
            throw new LoginException("注册金额不可为负");
        }
        bankuserMapper.insert(bankuser);
        banklogMapper.saveLog(new Banklog(bankuser.getName(), "注册：", LocalDateTime.now()));
        return true;
    }

    @Override
    public Boolean login(Bankuser bankuser) throws LoginException {
        if (!exist(bankuser)) {
            throw new LoginException("用户名不存在");
        }
        Bankuser bankuser1 = bankuserMapper.selectByName(bankuser.getName());
        if (bankuser1.getState() == 1) {
            throw new LoginException("账户已被冻结");
        }
        if (!bankuser.getPassword().equals(bankuser1.getPassword())) {
            throw new LoginException("账户密码错误");
        }
        bankuser.setBalance(bankuser1.getBalance());
        banklogMapper.saveLog(new Banklog(bankuser.getName(), "登录：", LocalDateTime.now()));
        return true;
    }

    @AOPTag
    @Override
    public Boolean destroyAccount(Bankuser bankuser) throws BusinessException {
        if (!exist(bankuser)) {
            throw new BusinessException("用户名不存在");
        }
        Bankuser bankuser1 = bankuserMapper.selectByName(bankuser.getName());
        if (bankuser1.getState() == 1) {
            throw new BusinessException("账户已被冻结");
        }
        bankuserMapper.deleteByName(bankuser.getName());
        banklogMapper.saveLog(new Banklog(bankuser.getName(), "销号：", LocalDateTime.now()));
        return true;
    }

    @Override
    public Boolean exist(Bankuser bankuser) {
        Bankuser bankuser1 = bankuserMapper.selectByName(bankuser.getName());
        return bankuser1 != null;
    }

    @AOPTag
    @Override
    public Boolean freeze(Bankuser bankuser, Bankuser bankuser2) throws NoPowerException, TransferErrorException {

        if ((bankuser.getName().equals(bankuser2.getName())) || (!exist(bankuser2))) {
            throw new TransferErrorException();
        }
        Bankuser bankuser1 = bankuserMapper.selectByName(bankuser2.getName());
        if (bankuser1.getState() == 1) {
            throw new TransferErrorException("对象已经处于冻结状态了");
        }
        if (bankuser.getName().equals("root")) {
            bankuserMapper.freeze(bankuser2.getName());
            banklogMapper.saveLog(new Banklog(bankuser.getName(), "冻结：" + bankuser2.getName(), LocalDateTime.now()));
            return true;
        }
        throw new NoPowerException();
    }

    @AOPTag
    @Override
    public Boolean unfreeze(Bankuser bankuser, Bankuser bankuser2) throws NoPowerException, TransferErrorException {
        if ((bankuser.getName().equals(bankuser2.getName())) || (!exist(bankuser2))) {
            throw new TransferErrorException();
        }
        Bankuser bankuser1 = bankuserMapper.selectByName(bankuser2.getName());
        if (bankuser1.getState() == 0) {
            throw new TransferErrorException("对象已经处于解冻状态了");
        }
        if (bankuser.getName().equals("root")) {
            bankuserMapper.unfreeze(bankuser2.getName());
            banklogMapper.saveLog(new Banklog(bankuser.getName(), "解冻：" + bankuser2.getName(), LocalDateTime.now()));
            return true;
        }
        throw new NoPowerException();
    }


}

